package cn.tedu.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCLogin3 {
    public static void main(String[] args) {
        try (Connection connection=DBUtil.getConnertion();){
            Scanner scanner = new Scanner(System.in);
            System.out.println("用户名");
            String username=scanner.nextLine();
            System.out.println("密码");
            String password=scanner.nextLine();
            System.out.println("昵称");
            String nickname=scanner.nextLine();
            System.out.println("年龄");
            Integer age=scanner.nextInt();
            String sql="SELECT id FROM userinfo WHERE username=?";
            PreparedStatement pr = connection.prepareStatement(sql);
            pr.setString(1,username);
            ResultSet r = pr.executeQuery();
            if (r.next()){
                System.out.println("用户已被占用");
            }else{
                String sql1="INSERT INTO userinfo(username,password,nickname,age) VALUES(?,?,?,?)";
                PreparedStatement pr1 = connection.prepareStatement(sql1);
                pr1.setString(1,username);
                pr1.setString(2,password);
                pr1.setString(3,nickname);
                pr1.setInt(4,age);
                int i=pr1.executeUpdate();  // 代表输入
                if(i>0)
                System.out.println("注册成功");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
